#include "pe_ccsprite.h"
#include "PE_CCSprite.h"
#include "CCQTSceneDirector.h"
#include "CCTextureCache.h"
#include "constantManager.h"

PE_CCSprite::PE_CCSprite(QtVariantPropertyManager *variantManager):PE_CCRGBAProtocol(variantManager)
{
    QDir  dir(ConstantManager::sharedManager()->getResourcePath());
	QStringList filter ;
	filter << "*.png";
	m_fileNameList = dir.entryList(filter);
	m_fileNameList.insert(0, "default");
}

QtProperty *PE_CCSprite::initUI()
{
    QtVariantProperty * item = addProperty(QtVariantPropertyManager::enumTypeId() , "Image");
//    QFile file("");
    item->setAttribute(QLatin1String("enumNames"), m_fileNameList);

    return PE_CCRGBAProtocol::initUI();
}

void PE_CCSprite::refresh()
{
    CCSprite * sprite = dynamic_cast<CCSprite*> (CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
    if(sprite ==NULL) return;
	int imageIndex = m_mapSprite2Imageindex.value((int)sprite);
	Property("Image")->setValue(imageIndex);
    PE_CCRGBAProtocol::refresh();
}

void PE_CCSprite::valueChanged(QtProperty *property, QVariant variant)
{
	CCSprite * sprite = dynamic_cast<CCSprite*> (CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
	if (property == Property("Image"))
	{
		QString fileName = m_fileNameList[variant.value<int>()];
		if (fileName == "default")
		{
			fileName = "ball.png";
		}
		m_mapSprite2Imageindex.insert((int) sprite,  variant.value<int>());
		CCRect rect = CCRectZero;
		CCTexture2D * pTexture = CCTextureCache::sharedTextureCache()->addImage(fileName.toLocal8Bit());
        rect.size = pTexture->getContentSize();
		sprite->setTexture(pTexture);
		sprite->setTextureRect(rect);
		PEFactory::shareFactory()->getPEditorByName("CCNode")->refresh();
	}
    PE_CCRGBAProtocol::valueChanged(property, variant);
}










